import { GET, POST } from '../axios.config.js'

const base = window.config.ip
const uiasBack = '/public-uias-back'

const publicApi = `${base}${uiasBack}/code-public`
const orgApi = `${base}${uiasBack}/uias-organ`
const logApi = `${base}${uiasBack}/uias-user-operate-record`
const userApi = `${base}${uiasBack}/uias-user`
const roleApi = `${base}${uiasBack}/uias-role`
const menuApi = `${base}${uiasBack}/uias-menu`
const postApi = `${base}${uiasBack}/uias-post`
const manageApi = `${base}/rcdg-zbkh/user-manage`
const delayApi = `${base}/public-uias-api/uias-tenant-delay`

const URLS = {
  listRecursiveOrgan: `${orgApi}/listRecursiveOrgan`, // 查询机构树
  saveOrgan: `${orgApi}/saveOrgan`, // 新增机构
  saveOrganByApi: `${orgApi}/saveOrganByApi`, // 新增机构
  editOrgan: `${orgApi}/editOrgan`, // 编辑机构
  delOrgan: `${orgApi}/delOrgan`, // 删除机构树
  updateOrderBy: `${orgApi}/updateOrderBy`, // 机构树拍苏
  getUiasOrgan: `${orgApi}/getUiasOrgan`, // 获取机构详情

  listUserOperLog: `${logApi}/listUserOperLog`, // 操作日志
  downLogFile: `${base}${uiasBack}/file-upload/downLogFile`, // 下载日志

  listUserData: `${userApi}/listUserData`, // 获取用户列表
  listFailureUserData: `${userApi}/listFailureUserData`, // 获取历史用户列表
  deleteUserInfo: `${userApi}/deleteUserInfo`, // 删除用户,
  getUiasUserDetail: `${userApi}/getUiasUserDetail`, // 用户详情
  saveOrEditUser: `${userApi}/saveOrEditUser`, // 保存或者编辑用户
  updateOrderByUser: `${userApi}/updateOrderBy`, // 调整人员顺序

  saveCode: `${publicApi}/saveCode`, // 保存字典信息
  listCodeData: `${publicApi}/listCodeData`, // 根据字典类型获取对应的字典数据
  updateCode: `${publicApi}/updateCode`, // 修改字典信息
  deleteCode: `${publicApi}/deleteCode`, // 删除字典信息

  listRoleData: `${roleApi}/listRoleData`, // 获取角色列表
  listTenantRoleData: `${roleApi}/listTenantRoleData`, // 获取角色列表-租户分层
  saveOrEditRole: `${roleApi}/saveOrEditRole`, // 新增或者修改角色
  getRoleDetail: `${roleApi}/getRoleDetail`, // 获取角色详情

  listUiasOrgPost: `${base}${uiasBack}/uias-org-post/listUiasOrgPost`, // 查询机构职位关联表
  listTenantDelay: `${delayApi}/listTenantDelay`, // 延期管理记录列表
  saveTenantDelayRecord: `${delayApi}/saveTenantDelayRecord`, // 延期管理记录列表
  updateDelayStatus: `${delayApi}/updateDelayStatus`, // 更新延期申请状态

  updateUserStatus: `${manageApi}/updateUserStatus`, // 更新用户列表
  selectMenuLevelWithAppTwNew: `${menuApi}/selectMenuLevelWithAppTwNew`, // 更新用户列表
  getUiasPostDetail: `${postApi}/getUiasPostDetail`, // 获取职位详情
  savePost: `${postApi}/savePost`, // 添加职位
  editPost: `${postApi}/editPost`, // 编辑职位信息
  delPost: `${postApi}/delPost`, // 删除职位
  importUiasUserDetail: `${userApi}/importUiasUserDetail`, // 导入批量用户
  getImportUserTemplate: `${userApi}/skipValidation/getImportUserTemplate`, // 下载导入模板
}

// 查询机构树 status:1-启用，0-禁用
export const listRecursiveOrgan = (type = '') =>
  GET(URLS.listRecursiveOrgan, {
    status: '1',
    tenantId: localStorage.pskh_tenantId,
    isGroup: 0,
  })

// 新增机构
export const saveOrgan = ({
  accountName = '',
  orgAlias = '',
  orgName = '',
  orgType = '',
  password = '',
  pid = '',
  postName = '',
  status = '1',
  telephone = '',
  loginName = '',
  governmentType = '',
  districtLevel = '',
  orgAddress = '',
  orgFunction = '',
}) =>
  POST(URLS.saveOrgan, {
    loginName,
    accountName,
    orgAlias,
    orgName,
    orgType,
    password,
    pid,
    postName,
    status,
    telephone,
    governmentType,
    districtLevel,
    orgAddress,
    orgFunction,
    tenantId: localStorage.pskh_tenantId,
  })
// 新增机构
export const saveOrganByApi = ({
  orgAlias = '',
  orgName = '',
  orgType = '',
  pid = '',
  postName = '',
  status = '',
  governmentType = '',
  districtLevel = '',
  orgAddress = '',
  orgFunction = '',
  id = '',
}) =>
  POST(URLS.saveOrganByApi, {
    inputVOS: [
      {
        orgAlias,
        orgName,
        orgType,
        pid,
        postName,
        status,
        governmentType,
        districtLevel,
        orgAddress,
        orgFunction,
        id,
        tenantId: localStorage.pskh_tenantId,
      },
    ],
  })

// 编辑机构
export const editOrgan = ({
  loginName = '',
  accountName = '',
  orgAlias = '',
  orgName = '',
  orgType = '',
  password = '',
  id = '',
  pid = '',
  postName = '',
  status = '1',
  telephone = '',
  governmentType = '',
  districtLevel = '',
  orgAddress = '',
  orgFunction = '',
}) =>
  POST(URLS.editOrgan, {
    loginName,
    accountName,
    orgAlias,
    orgName,
    orgType,
    password,
    id,
    pid,
    postName,
    status,
    telephone,
    governmentType,
    districtLevel,
    orgAddress,
    orgFunction,
    tenantId: localStorage.pskh_tenantId,
  })

// 删除机构树
export const delOrgan = id => GET(URLS.delOrgan, { id })

// 机构树排序
export const updateOrderBy = ({ operate, operateId, targetId }) =>
  POST(URLS.updateOrderBy, { operate, operateId, targetId, tenantId: localStorage.pskh_tenantId })

// 获取机构详情
export const getUiasOrgan = ({ id }) => GET(URLS.getUiasOrgan, { id })

// 操作日志
export const listUserOperLog = ({ endTime = '', key = '', pageNum = 1, pageSize = 10, startTime = '' }) =>
  POST(URLS.listUserOperLog, {
    endTime,
    key,
    pageNum,
    pageSize,
    startTime,
    tenantId: localStorage.pskh_tenantId,
  })

// 下载日志
export const downLogFile = () =>
  GET(
    URLS.downLogFile,
    {},
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    }
  )

// 保存字典信息
export const saveCode = ({ codeType = '', name = '' }) =>
  POST(URLS.saveCode, { codeType, name, tenantId: localStorage.pskh_tenantId })

// 根据字典类型获取对应的字典数据
export const listCodeData = type => GET(URLS.listCodeData, { codeType: type, tenantId: localStorage.pskh_tenantId })

// 修改字典信息
export const updateCode = ({ code = '', name = '', codeType = '' }) => POST(URLS.updateCode, { code, name, codeType })

// 删除字典信息
export const deleteCode = ({ codeType, code }) => POST(URLS.deleteCode, { codeType, code })

// 获取用户列表
export const listUserData = ({ pageNum = 1, pageSize = 10, key = '', orgId = '', status = '', tenantId = '' }) =>
  GET(URLS.listUserData, {
    pageNum,
    pageSize,
    key,
    orgId,
    status,
    tenantId: localStorage.pskh_tenantId,
  })

// 获取历史用户列表
export const listFailureUserData = ({ pageNum = 1, pageSize = 10, key = '', orgId = '', status = '', tenantId = '' }) =>
  GET(URLS.listFailureUserData, {
    pageNum,
    pageSize,
    key,
    orgId,
    status,
    tenantId: localStorage.pskh_tenantId,
  })

// 获取用户详情
export const getUiasUserDetail = id => GET(URLS.getUiasUserDetail, { id })

// 删除用户
export const deleteUserInfo = id => GET(URLS.deleteUserInfo, { id })

// 保存或者编辑用户
export const saveOrEditUser = ({
  id,
  userName,
  loginName,
  loginPassword,
  headUrl,
  telephone,
  fixedphone,
  email,
  gender,
  birthday,
  politicalOutlook,
  education,
  userCategory,
  mainLeader,
  mainLeadership,
  userIdentity,
  userStatus = '2',
  roleIds,
  orgPostList,
  postDuty = '',
  status = '1',
  leadChargeDepart,
}) =>
  POST(URLS.saveOrEditUser, {
    id,
    loginName,
    loginPassword,
    postDuty,
    status,
    userStatus,
    telephone,
    userName,
    roleIds,
    orgPostList,
    leadChargeDepart,
    fixedphone,
    email,
    birthday,
    gender,
    userCategory,
    mainLeader,
    mainLeadership,
    userIdentity,
    headUrl,
    politicalOutlook,
    education,
    tenantId: localStorage.pskh_tenantId,
  })

// 新增或者修改角色
export const updateOrderByUser = ({ operate, operateId, targetId, tenantId }) =>
  POST(URLS.updateOrderByUser, {
    operate,
    operateId,
    targetId,
    tenantId,
  })

// 获取角色列表 status：1-启用，0-禁用
export const listRoleData = (status = 1) => GET(URLS.listRoleData, { status, tenantId: localStorage.pskh_tenantId })

// 获取角色列表-租户分层
export const listTenantRoleData = (status = 1) =>
  GET(URLS.listTenantRoleData, { status, tenantId: localStorage.pskh_tenantId })

// 新增或者修改角色
export const saveOrEditRole = ({ status = 1, id, menuIds = [], remark = '', roleCode, roleName, roleType = 2 }) =>
  POST(URLS.saveOrEditRole, {
    id,
    status,
    menuIds,
    remark,
    roleCode,
    roleName,
    roleType,
    tenantId: localStorage.pskh_tenantId,
  })

// 获取角色详情
export const getRoleDetail = ({ id }) =>
  GET(URLS.getRoleDetail, {
    id,
    tenantId: localStorage.pskh_tenantId,
  })

// 查询机构职位关联表
export const listUiasOrgPost = ({ pageNum = 1, pageSize = 10, orgId = '', postName = '' }) =>
  GET(URLS.listUiasOrgPost, {
    pageNum,
    pageSize,
    orgId,
    postName,
    tenantId: localStorage.pskh_tenantId,
  })

// 延期管理记录列表
export const listTenantDelay = ({ status = '', tenantId = '', userId = '', keywords = '' }) =>
  POST(URLS.listTenantDelay, {
    keywords,
    status,
    tenantId: tenantId,
    userId,
  })

// 保存延期管理
export const saveTenantDelayRecord = ({ orgName, tenantId, userId, userName }) =>
  POST(URLS.saveTenantDelayRecord, {
    orgName,
    tenantId: tenantId || localStorage.pskh_tenantId,
    userId: userId || localStorage.pskh_userId,
    userName,
  })

// 更新延期申请状态
export const updateDelayStatus = ({ delayTime, id, status }) =>
  POST(URLS.updateDelayStatus, {
    delayTime,
    id,
    status,
  })

// 更新用户列表
export const updateUserStatus = ({ id, status = '2' }) =>
  POST(URLS.updateUserStatus, {
    id,
    status,
  })

// 获取用户菜单权限
export const getMenuList = () =>
  POST(URLS.selectMenuLevelWithAppTwNew, {
    fieldName: '1',
    id: '',
  })

// 获取职位详情
export const getUiasPostDetail = id =>
  GET(URLS.getUiasPostDetail, {
    id,
  })

// 添加职位
export const savePost = ({ id, postName, postAlias, postCode, postLevel, remark, postId, roleIds, relationOrg }) =>
  POST(URLS.savePost, {
    postName,
    postAlias,
    postCode,
    postLevel,
    remark,
    postId,
    roleIds,
    relationOrg,
    tenantId: localStorage.pskh_tenantId,
  })

// 编辑职位信息
export const editPost = ({ id, postName, postAlias, postCode, postLevel, remark, postId, roleIds, relationOrg }) =>
  POST(URLS.editPost, {
    id,
    postName,
    postAlias,
    postCode,
    postLevel,
    remark,
    postId,
    roleIds,
    relationOrg,
    tenantId: localStorage.pskh_tenantId,
  })

// 获取职位详情
export const delPost = id =>
  GET(URLS.delPost, {
    id,
  })

// 批量导入用户
export const importUiasUserDetail = fmData => POST(URLS.importUiasUserDetail, fmData)

// 批量导入用户
export const getImportUserTemplate = () => `${URLS.getImportUserTemplate}`
